package com.slofzx.nuoda.evaluation.param.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.slofzx.nuoda.common.util.IdGen;
import com.slofzx.nuoda.evaluation.param.dto.CommonUnitCostDTO;
import com.slofzx.nuoda.evaluation.param.entity.CommonBaseIrr;
import com.slofzx.nuoda.evaluation.param.dto.CommonBaseIrrDTO;
import com.slofzx.nuoda.evaluation.param.entity.CommonUnitCost;
import com.slofzx.nuoda.evaluation.param.vo.CommonBaseIrrVO;
import com.slofzx.nuoda.evaluation.param.dao.CommonBaseIrrDAO;
import com.slofzx.nuoda.evaluation.param.service.CommonBaseIrrService;
import com.slofzx.nuoda.evaluation.project.util.BusiUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.slofzx.nuoda.common.enums.CommonResultEnum;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 公共基准收益率服务实现类
 *
 * @author nuoda
 * @since 2023-10-17 16:00:34
 */
@Service("commonBaseIrrService")
public class CommonBaseIrrServiceImpl extends BaseServiceImpl implements CommonBaseIrrService {
    @Autowired
    private CommonBaseIrrDAO commonBaseIrrDAO;

    /**
     * 将数据表数据转换为返回数据类型
     *
     * @param entity 数据表对应类
     * @return 实例对象
     */
    protected CommonBaseIrrVO parseEntity2VO(CommonBaseIrr entity) {
        CommonBaseIrrVO vo = new CommonBaseIrrVO();
        vo.setId(entity.getId());
        vo.setVersionId(entity.getVersionId());
        vo.setPoolType(entity.getPoolType());
        vo.setValue(entity.getValue());
        vo.setCreateUser(entity.getCreateUser());
        vo.setCreateTime(entity.getCreateTime());
        return vo;
    }

    /**
     * 将数据传递类转换为数据表数据类
     *
     * @param dto 数据传递类实例
     * @return 实例对象
     */
    protected CommonBaseIrr parseDTO2Entity(CommonBaseIrrDTO dto) {
        CommonBaseIrr entity = new CommonBaseIrr();
        entity.setId(dto.getId());
        entity.setVersionId(dto.getVersionId());
        entity.setPoolType(dto.getPoolType());
        entity.setValue(dto.getValue());
        return entity;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CommonBaseIrrVO queryById(String id) {
        CommonBaseIrr entity = this.commonBaseIrrDAO.selectById(id);
        if (entity == null) {
            return null;
        }
        return parseEntity2VO(entity);
    }

    /**
     * 查询多条数据
     *
     * @param dto 数据传递类实例对象
     * @return 对象列表
     */
    @Override
    public List<CommonBaseIrrVO> queryList(CommonBaseIrrDTO dto) {
        //mybatis-plus 写法示例
        //IPage<CommonBaseIrr> pageResult = this.evaModelDAO.selectPage(new Page<>(dto.getPageNo(),dto.getPageSize()),null);
        //List<CommonBaseIrr> dataList = pageResult.getRecords();
        //dto.setCount((int) pageResult.getTotal());
        List<CommonBaseIrr> dataList = this.commonBaseIrrDAO.queryList(dto);
        if (null == dataList || dataList.isEmpty()) {
            return Collections.emptyList();
        }
        List<CommonBaseIrrVO> resultList = new ArrayList(dataList.size());
        for (CommonBaseIrr item : dataList) {
            CommonBaseIrrVO vo = parseEntity2VO(item);
            resultList.add(vo);
        }
        // TODO 添加其他关联查询数据
        return resultList;
    }

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult create(CommonBaseIrrDTO dto) {
        // TODO 添加校验逻辑
        CommonBaseIrr entity = parseDTO2Entity(dto);
        preInsert(entity);
        this.commonBaseIrrDAO.insert(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult update(CommonBaseIrrDTO dto) {
        String id = dto.getId();
        if (StringUtils.isEmpty(id)) {
            return CommonResultEnum.NOT_EXIST;
        }
        CommonBaseIrr entity = this.commonBaseIrrDAO.selectById(id);
        if (entity == null) {
            return CommonResultEnum.NOT_EXIST;
        }
        // TODO 添加其他校验逻辑，如数据权限

        entity.setVersionId(dto.getVersionId());
        entity.setPoolType(dto.getPoolType());
        entity.setValue(dto.getValue());
        this.commonBaseIrrDAO.updateById(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 操作结果
     */
    @Override
    public ServiceResult deleteById(String id) {
        // TODO 添加校验逻辑，如数据权限
        this.commonBaseIrrDAO.deleteById(id);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    @Transactional
    public ServiceResult save(String versionId, List<CommonBaseIrrDTO> dtoList) {
        if (dtoList == null || dtoList.isEmpty()) {
            return CommonResultEnum.SUCCESS;
        }
        this.deleteByVersion(versionId);
        List<CommonBaseIrr> insertList = new ArrayList<>();
        for (CommonBaseIrrDTO dto : dtoList) {
            CommonBaseIrr entity = parseDTO2Entity(dto);
            preInsert(entity);
            entity.setVersionId(versionId);
            insertList.add(entity);
        }
        commonBaseIrrDAO.insertBatch(insertList);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public ServiceResult deleteByVersion(String versionId) {
        QueryWrapper<CommonBaseIrr> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CommonBaseIrr::getVersionId, versionId);
        commonBaseIrrDAO.delete(queryWrapper);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public List<CommonBaseIrrVO> queryListByVersion(String versionId) {
        CommonBaseIrrDTO commonBaseIrrDTO = new CommonBaseIrrDTO();
        commonBaseIrrDTO.setVersionId(versionId);
        return queryList(commonBaseIrrDTO);
    }

    @Override
    public void copy(String fromId, String toId) {
        final CommonBaseIrrDTO dto = new CommonBaseIrrDTO();
        dto.setVersionId(fromId);
        final List<CommonBaseIrr> commonBaseIrrs = commonBaseIrrDAO.queryList(dto);
        if (CollectionUtils.isEmpty(commonBaseIrrs)) {
            return;
        }

        final List<CommonBaseIrr> collect = commonBaseIrrs.stream().map(m -> {
            final CommonBaseIrr deepCopy = BusiUtil.deepCopy(m, CommonBaseIrr.class);
            deepCopy.setId(IdGen.uuid());
            deepCopy.setCreateTime(new Date());
            deepCopy.setVersionId(toId);
            return deepCopy;
        }).collect(Collectors.toList());

        commonBaseIrrDAO.insertBatch(collect);

    }


}
